package com.qf.huigou.controller;

import com.qf.huigou.commons.ParamVo;
import com.qf.huigou.commons.SystemParamTool;
import com.qf.huigou.pojo.*;
import com.qf.huigou.service.BrandService;
import com.qf.huigou.service.CategoryService;
import com.qf.huigou.service.GoodService;
import com.qf.huigou.service.PropertyService;
import com.qf.huigou.utils.ParseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 1.8.0_131
 * @Author WangYiZhu
 * @Email 2641956765@qq.com
 * @Time 2022/12/08 11:33
 */
@Controller
@RequestMapping("good")
public class GoodController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private BrandService brandService;
    @Autowired
    private GoodService goodService;
    @Autowired
    private PropertyService propertyService;

    @RequestMapping(value = {"home", "query", "querybrand"})
    public String home(@RequestParam(defaultValue = "1") int categoryId, @RequestParam(defaultValue = "-1") int brandId, Model model) {
        List<Category> categoryList = categoryService.findCategoryAll();
        List<Brand> brandList = brandService.findBrandToCategoryId(categoryId);
        List<Good> goodList = null;
        if (brandId == -1) {
            //默认打开页面查找所有categoryId
            goodList = goodService.findGoodByCategoryOrBrandId(categoryId, -1);
        } else {
            //二级分类查找
            goodList = goodService.findGoodByCategoryOrBrandId(-1, brandId);
        }

        model.addAttribute("categoryList", categoryList);
        model.addAttribute("brandList", brandList);
        model.addAttribute("goodList", goodList);
        return "home";
    }

    @RequestMapping("info")
    public String info(int goodId, Model model) {
        Good good = goodService.getGoodToId(goodId);
        List<Map<ParProperty, List<SubProperty>>> mapList = propertyService.getMapListToGoodId(goodId);
        model.addAttribute("mapList", mapList);
        model.addAttribute("good", good);
        return "info";
    }

    @RequestMapping("calc")
    public String calcInfo(int goodId, int[] subTypeIds, Model model, HttpSession session) {
        //需要加载购物车信息
        List<Cart> cartList = null;
        Integer totalPrice = 0;
        User user = (User) session.getAttribute("USER");
        Good good = goodService.getGoodToId(goodId);
        if (user != null) { //有用户登录，查询数据库
            //购物车信息从数据库中读取
            cartList = goodService.searchAllCart(user.getUserId());

            for (int i = 0; i < cartList.size(); i++) {
                totalPrice += cartList.get(i).getCartPrice();
            }
        } else {
            //读取当前项目缓存
            cartList = SystemParamTool.cartList;
            for (int i = 0; i < cartList.size(); i++) {
                totalPrice += cartList.get(i).getCartPrice();
            }
        }
        model.addAttribute("cartList", cartList);
        int goodPrice = propertyService.getPriceToGood(goodId, subTypeIds);
        model.addAttribute("integers", ParseUtils.parseIntoList(goodPrice));
        model.addAttribute("good", good);
        model.addAttribute("totalPrice", totalPrice);
        model.addAttribute("goodPrice", goodPrice);
        model.addAttribute("subTypeIds", subTypeIds);
        return "show_price";
    }

    @RequestMapping("addcart")
    @ResponseBody
    public Map<String, String> addCart(int goodId, int goodPrice, HttpSession session) {
        Map<String, String> map = new HashMap<>();
        Cart cart = new Cart();
        cart.setCartPrice(goodPrice);
        cart.setGood(goodService.getGoodToId(goodId));
        boolean addOk = false;
        //从查看当前有没有用户登录
        User user = (User) session.getAttribute("USER");
        if (user != null) { //有用户登录，查询数据库
            //还需要添加一个用户信息
            addOk = goodService.addToCar2(user.getUserId(),goodId,goodPrice);
            if (addOk){
                map.put("msg", "success");
            }else {
                map.put("msg","err");
            }
        } else { //无登录，临时存储购物车数据
            //cart 需要自己设计一个id
            cart.setCartId(++SystemParamTool.cartId);
            SystemParamTool.cartList.add(cart);
            map.put("msg", "success");
        }

        return map;
    }

    @RequestMapping("deletecart")
    @ResponseBody
    public Map<String, String> deletecart(int cartId, HttpSession session) {
        Map<String, String> map = new HashMap<>();
        Cart cart = null;
        //从查看当前有没有用户登录
        Object user = session.getAttribute("USER");
        if (user != null) { //有用户登录，查询数据库
        int i= goodService.deleteByCartId(cartId);
        //删除成功
        if (i==1){
            map.put("msg", "success");
        }
            //还需要添加一个用户信息
        } else { //无登录，临时存储购物车数据
            //cart
            //System.out.println("****"+SystemParamTool.cartList);
            for (int i = 0; i < SystemParamTool.cartList.size(); i++) {
                if (SystemParamTool.cartList.get(i).getCartId() == cartId) {
                    cart = SystemParamTool.cartList.get(i);
                }
            }
            SystemParamTool.cartList.remove(cart);
            map.put("msg", "success");
        }
        return map;
    }


    @RequestMapping("order")
    public String KK() {
        return "order";
    }


}
